
var reverse = function(x) {
    let str = ''
    if(x < 0){
        x = Math.abs(x)
    }
    let length = String(x).length
    for(let i=0;i < length;i++){
        str += x%10
        x = Math.floor( x/10)
    }
    if(str[str.length-1] === '-') str = -Number(str) 
    if(str < Math.pow(-2,31) || str > Math.pow(2,31) - 1){
        return 0
    }
    return str
};
console.log(reverse(-12345));